<?php

class Am_Newsletter_Plugin_Arp extends Am_Newsletter_Plugin
{
    protected function _initSetupForm(Am_Form_Setup $form)
    {
        $el = $form->addText('url', array('size' => 60))->setLabel('AutoResponse Pro Url');
        $el->addRule('required');
        $el->addRule('regex', 'URL must start with http:// or https://', '/^(http|https):\/\//');
    }
    
    function doRequest(array $vars)
    {
        $req = new Am_HttpRequest($this->getConfig('url'), Am_HttpRequest::METHOD_POST);
        $req->addPostParameter($vars);
        return $req->send();
    }
    
    public function isConfigured()
    {
        return strlen($this->getConfig('url'));
    }

    public function subscribeUser(User $user, $listId)
    {
        $ret = $this->doRequest(array(
            'id' => $listId,
            'full_name' => $user->getName(),
            'split_name' => $user->getName(),
            'email' => $user->getEmail(),
            'subscription_type' => 'E',
        ));
    }

    public function unsubscribeUser(User $user, $listId)
    {
        $ret = $this->doRequest(array(
            'id' => $listId,
            'full_name' => $user->getName(),
            'split_name' => $user->getName(),
            'email' => $user->getEmail(),
            'subscription_type' => 'E',
            'arp_action=UNS',
        ));
    }
    
}